package com.ogawa.joypal.distributionnetwork.viewmodel

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.ogawa.base.base.BaseApp
import com.ogawa.base.base.BaseViewModel
import com.ogawa.base.bean.LoadState
import com.ogawa.projectcommon.utils.dataConvert
import com.ogawa.base.utils.isEmptyStr
import com.ogawa.projectcommon.utils.launch
import com.ogawa.projectcommon.network.RetrofitManager
import com.ogawa.joypal.distributionnetwork.MainApiService
import com.ogawa.joypal.distributionnetwork.R
import com.ogawa.joypal.distributionnetwork.bean.BindUserDeviceResponse
import com.ogawa.joypal.distributionnetwork.bean.BindUserDevicesRequest
import com.ogawa.projectcommon.bean.EntityUserDevice
import com.ogawa.projectcommon.dboperation.OperationDB
import com.ogawa.projectcommon.utils.ProjectSpUtils

class SelectRoomViewModel : BaseViewModel() {
    var mBindUserDevicesRequest = BindUserDevicesRequest()
    val mBindUserDeviceResponse = MutableLiveData<BindUserDeviceResponse>()
//    var bindSnState = MutableLiveData<String>()

    fun bindUserDevices() {
        launch({
            loadState.value = LoadState.Loading("")
            var userInfo = ProjectSpUtils.getUserInfo()

            if (userInfo.isVisitor){
                val queryEntityUserDeviceByDeviceID =
                    OperationDB().queryEntityUserDeviceBySn(mBindUserDevicesRequest.sn)
                if (queryEntityUserDeviceByDeviceID != null && isEmptyStr(queryEntityUserDeviceByDeviceID.deviceSN)){
                    ToastUtils.showShort(BaseApp.appContext.getString(R.string.device_relay_add))
                }else{
                    val projectConfigWifiDeviceBean =
                        ProjectSpUtils.getProjectConfigWifiDeviceBean()

                    var mEntityUserDevice = EntityUserDevice()
                    mEntityUserDevice.deviceId = mBindUserDevicesRequest.sn
                    mEntityUserDevice.userId = userInfo.user.id
                    mEntityUserDevice.deviceName = mBindUserDevicesRequest.name
                    mEntityUserDevice.deviceSN = mBindUserDevicesRequest.sn
                    mEntityUserDevice.readUuid = mBindUserDevicesRequest.readUuid
                    mEntityUserDevice.writerUuid = mBindUserDevicesRequest.writerUuid
                    mEntityUserDevice.peripheralUuid = mBindUserDevicesRequest.peripheralUuid
                    mEntityUserDevice.iosUuid = mBindUserDevicesRequest.iosUuid
                    mEntityUserDevice.androidUuid = mBindUserDevicesRequest.androidUuid
                    mEntityUserDevice.bluetoothName = mBindUserDevicesRequest.bluetoothName
                    mEntityUserDevice.label = mBindUserDevicesRequest.label

                    mEntityUserDevice.typeCode = projectConfigWifiDeviceBean.typeCode
                    mEntityUserDevice.typeName = projectConfigWifiDeviceBean.typeName
                    mEntityUserDevice.imageSrc = projectConfigWifiDeviceBean.imageSrc
                    mEntityUserDevice.id = projectConfigWifiDeviceBean.id.toString()


                    OperationDB().addEntityUserDevice(mEntityUserDevice)
                    mBindUserDeviceResponse.value = BindUserDeviceResponse()
                }
            }else{
                val dataConvert = RetrofitManager.INSTANCE
                    .createService(MainApiService::class.java)
                    .bindUserDevices(mBindUserDevicesRequest)
                    .dataConvert()
                mBindUserDeviceResponse.value = dataConvert
            }



            loadState.value = LoadState.Success("")
//            bindSnState.value = Constant.SUCCESS
        }
            , { loadState.value = LoadState.Fail(it.message ?: "") }
            , {})
    }
}